<?php
namespace App\Http\Controllers\Job;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;

class IndexController extends \App\Http\Controllers\JobController {
    private $user;
    /**
     * 首页
     */
    public function __construct() {
        parent::__construct();
        $this->user = session('job');
    }
    public function index(Request $request) {
        return view('job.index.index', ['user'=>$this->user]);
    }
    
    /**
     * 我的任务
     */
    public function myJob(Request $request) {
        if ($request->has('jnum')) {
            $jobs = \App\Models\Myjob::where('dd_myjob.iperid', $this->user->id)->where('dd_jobs.jnum', $request->input('jnum'))
                ->leftJoin('dd_jobs', 'dd_myjob.jobid', '=', 'dd_jobs.id')
                ->select('dd_jobs.plotid', 'dd_jobs.cname', 'dd_jobs.cphone', 'dd_jobs.btype', 'dd_jobs.address', 'dd_jobs.jnum', 'dd_myjob.ctime', 'dd_myjob.id')
                ->get();
            
        } else {
            $jobs = \App\Models\Myjob::where('dd_myjob.status', 0)->where('dd_myjob.jstatus', 0)->where('dd_myjob.iperid', $this->user->id)
                ->leftJoin('dd_jobs', 'dd_myjob.jobid', '=', 'dd_jobs.id')
                ->select('dd_jobs.plotid', 'dd_jobs.cname', 'dd_jobs.cphone', 'dd_jobs.btype', 'dd_jobs.address', 'dd_jobs.jnum', 'dd_myjob.ctime', 'dd_myjob.id')
                ->get();
        }
        $msg = '';
        if (!count($jobs)) {
            $msg = "没有任务";
        }
        $assign = compact('jobs', 'request', 'msg');
        return view('job.index.myjob', $assign);
    }
    /**
     * 任务详细信息
     */
    public function jobDetail($id) {
        $job = \App\Models\Myjob::where('dd_myjob.id', $id)->where('iperid', $this->user->id)
                ->leftJoin('dd_jobs', 'dd_myjob.jobid', '=', 'dd_jobs.id')
                ->select('dd_jobs.plotid', 'dd_jobs.cname', 'dd_jobs.cphone', 'dd_jobs.btype', 'dd_jobs.address', 'dd_jobs.jnum', 'dd_myjob.ctime', 'dd_myjob.id', 'dd_myjob.status', 'dd_myjob.jstatus', 'dd_myjob.jobid', 'dd_myjob.iperid')
                ->first();
        $assign = compact('job');
        return view("job.index.jobdetail", $assign);
    }
    /**
     * 问题反馈
     */
    public function tick(Request $request, $id) {
        $msg = "";
        if (($request->has('causeid') && $request->has('suggest'))) {
            \App\Models\Myjob::updatetick($request, $id);
            return redirect("job/index/jobdetail/{$id}");
        }
        if ($request->ajax()) {
            return \Illuminate\Support\Facades\Response::json(\App\Models\Myjob::updatetick($request, $id));
        }
        $causes = \App\Models\Cause::get();
        $assign = compact('causes', 'id', 'request', 'msg');
        return view('job.index.tick', $assign);
    }
    /**
     * 公告
     */
    public function notice() {
        $notices = \App\Models\Notice::orderBy('ctime', 'desc')->paginate(5);
        return view('job.notice.index', ['notices'=>$notices]);
    }
}
